module ApplicationHelper

  # 搜索表单的按钮
  def search_button f
    render partial: '/share/search_button', locals: {f: f}
  end

  def html_buttons &block
    content_for :html_buttons , &block
  end

  def crumbs &block
    content_for :crumbs , &block
  end

  # 清空按钮
  def reset_button btn_size='xs'
    link_to t(:empty), 'javascript:void(0)', class: "btn btn-#{btn_size} btn-info push-5-l", type: 'reset'
  end

  # 分页器，功能：左侧显示上下页、指定页面跳转、每页数据数量切换，右侧只显示总数
  def paginator_bottom objs
    render partial: '/share/paginator_bottom', locals: {objs: objs}
  end

  def modal_buttons &block
    content_for :modal_buttons , &block
  end

  def close_modal_button
    link_to t(:close), 'javascript:void(0)', class: 'btn btn-default btn-md push-20-l', data: {dismiss: 'modal'}
  end

  def truncate_str(str, pop_str=nil, length=20)
    link_to truncate(str, length: length), 'javascript:void(0);',
            'data'=> {'toggle'=> "tooltip",'placement'=> 'right', 'html'=> 'true',
                      'original-title'=> pop_str||str}
  end

  # bootstrap的modal弹框，统一用的一个div#application-modal，每次modal关闭的时候会清空内容
  def link_to_dialog name, url, **args
    args[:data] ||= {}
    args[:data].merge!(toggle: 'modal', target: '#application-modal', backdrop: 'static')
    link_to name, url, args
  end

  # 中型号的弹窗
  def link_to_md_dialog name, url, **args
    args[:data] ||= {}
    args[:data].merge!(toggle: 'modal', target: '#application-md-modal')
    link_to name, url, args
  end

  # 小型号的弹窗
  def link_to_sm_dialog name, url, **args
    args[:data] ||= {}
    args[:data].merge!(toggle: 'modal', target: '#application-sm-modal')
    link_to name, url, args
  end

  def has_permission?(obj, action)
    current_user.has_permission?(obj, action)
  end

  def boolean_icon b
    return '' if b.to_s.blank?
    (b ? "<i class='fa fa-check text-success'></i>" : "<i class='fa fa-close text-danger'></i>").html_safe
  end

end
